<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>设置我的密码</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="../../layui/css/layui.css" media="all">
  <link rel="stylesheet" href="../../css/admin.css" media="all">
</head>
<body>

  <div class="layui-fluid">
    <div class="layui-row layui-col-space15">
      <div class="layui-col-md12">
        <div class="layui-card">
          <div class="layui-card-header">修改密码</div>

          <div class="layui-card-body" pad15>
            <form class="layui-form" action="">
              <div class="layui-form-item">
                <label class="layui-form-label" style="width: 150px;">当前密码：</label>
                <div class="layui-input-block">
                  <input style="width:250px;" type="password" name="oldPassword" lay-verify="required|oldPassword" autocomplete="off" placeholder="请求当前密码" class="layui-input">
                </div>
              </div>
              <div class="layui-form-item">
                <label class="layui-form-label" style="width: 150px;">新密码：</label>
                <div class="layui-input-block">
                  <input style="width:250px;" type="password" name="newpassword" lay-verify="required|newpassword" autocomplete="off" placeholder="请输入新密码" class="layui-input">
                </div>
              </div>
              <div class="layui-form-item">
                <label class="layui-form-label" style="width: 150px;">确认新密码：</label>
                <div class="layui-input-block">
                  <input style="width:250px;" type="password" name="renewpassword" lay-verify="required|renewpassword" autocomplete="off" placeholder="请确认新密码" class="layui-input">
                </div>
              </div>
              <div class="layui-form-item">
                <div class="layui-input-block">
                  <button type="submit" class="layui-btn" lay-submit="" lay-filter="setmypass">确认修改</button>
                </div>
              </div>
            </form>
          </div>

        </div>
      </div>
    </div>
  </div>
  <!--引入本地的jQuery.js-->
  <script type="text/javascript" src="../../js/jquery-3.4.1.min.js"></script>
  <script src="../../layui/layui.js"></script>
  <script type="text/javascript">
      layui.use(['form','layer'], function(){
          var $ = layui.$
              ,layer = layui.layer
              ,form = layui.form;
          var $body = $('body');
          //自定义验证
          form.verify({
              oldPassword:function(value){
                  var reg = /^[\S]{6,12}$/;
                  if(!reg.test(value)){
                      return '密码必须6到12位，且不能出现空格';
                  }
                  //判断密码在数据库中是否一致
                  var user = JSON.parse(sessionStorage.getItem("user"));
                  var flag = false;
                  $.ajax({
                      url:"/checkPassword",
                      type:"post",
                      data:{login_account:user.login_account,user_pwd:value},
                      dataType:"json",
                      async:false, //如果定义全局变量，需要将其改同步方式，默认true.
                      success:function (res) {
                          if(res.code == 500){//当前密码与登录账户密码不符
                              flag = true;
                          }
                      }
                  });
                  if(flag){
                      return "当前密码与登录账户密码不符";
                  }
              }
              //数组的两个值分别代表：[正则匹配、匹配不符时的提示文字]
              ,newpassword:[
                  /^[\S]{6,12}$/
                  ,'密码必须6到12位，且不能出现空格'
              ]
              ,renewpassword:function(value,item){
                 var  newpassword = $("input[name=newpassword]").val();
                 if(!(value == newpassword)){
                     return "两次输入密码不一致";
                 }
              }
          });

          //监听提交
          form.on('submit(setmypass)', function(data){
              var user = JSON.parse(sessionStorage.getItem("user"));
              $.ajax({
                  url:"/updatePassword",
                  type:"post",
                  data:{login_account:user.login_account,newpassword:data.field.newpassword},
                  dataType:"json",
                  async:false,
                  success:function (res) {
                       if(res.code == 200){
                           layer.msg(res.msg, {
                               offset: '15px'
                               ,icon: 1
                               ,time: 1000
                           }, function(){
                               //清空登录信息
                               sessionStorage.removeItem("user");
                               window.parent.location.href = "/login.html";
                           });
                       }
                  }
              });
              return false;
          });

      });
  </script>

</body>
</html>